package com.bappi.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;

import com.tos.quran.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Parcelable;
import android.view.inputmethod.InputMethodManager;

public class Utils {

	public static void createShortCut(Context context, Class entryClass) {
		try {
			SharedPreferences sp = context.getSharedPreferences(
					context.getPackageName(), Context.MODE_PRIVATE);
			boolean isExists = sp.getBoolean("INSTALLED", false);
			if (!isExists) {
				sp.edit().putBoolean("INSTALLED", true).commit();
				Intent shortcutintent = new Intent(
						"com.android.launcher.action.INSTALL_SHORTCUT");
				shortcutintent.putExtra("duplicate", false);
				shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
						context.getString(R.string.app_name));
				Parcelable icon = Intent.ShortcutIconResource
						.fromContext(context.getApplicationContext(),
								R.drawable.ic_launcher);
				shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
						icon);
				shortcutintent
						.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
								context.getApplicationContext(), entryClass));
				context.sendBroadcast(shortcutintent);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void showKeyboard(Activity activity) {
		try {
			InputMethodManager imm = (InputMethodManager) activity
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			if (imm != null) {
				imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void hideKeyboard(Activity activity) {
		try {
			InputMethodManager inputManager = (InputMethodManager) activity
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			if ((inputManager != null) && (activity.getCurrentFocus() != null)
					&& (activity.getCurrentFocus().getWindowToken() != null)) {
				inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static final boolean isShareable(String transCode) {
		boolean ret = true;
		if (Constants.LANG_CODE_BENGALI.equals(transCode)
				|| Constants.LANG_CODE_HINDI.equals(transCode)
				|| Constants.LANG_CODE_PERSIAN.equals(transCode)) {
			ret = false;
		}
		return ret;
	}

	public static void showAlertDialogYesNo(Context context, String message,
			DialogInterface.OnClickListener yListener,
			DialogInterface.OnClickListener nListener) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message).setCancelable(false)
				.setPositiveButton(R.string.yes, yListener)
				.setNegativeButton(R.string.no, nListener);
		final AlertDialog alert = builder.create();
		alert.show();
	}

	public static void showAlertDialog(Context context, String message,
			DialogInterface.OnClickListener ocl) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message).setCancelable(false)
				.setPositiveButton(R.string.ok, ocl);
		final AlertDialog alert = builder.create();
		alert.show();
	}

	public static void showAlertDialog(Context context, String message) {
		showAlertDialog(context, message,
				new DialogInterface.OnClickListener() {
					public void onClick(final DialogInterface dialog,
							final int id) {

					}
				});
	}

	public static ProgressDialog getProgressDialog(Context context,
			String message, String title, String buttonText,
			DialogInterface.OnClickListener onClickListener) {
		ProgressDialog pDialog = new ProgressDialog(context);
		pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pDialog.setProgress(0);
		pDialog.setTitle(title);
		pDialog.setMessage(message);
		pDialog.setCancelable(false);
		pDialog.setMax(100);
		if (buttonText != null && onClickListener != null) {
			pDialog.setButton(buttonText, onClickListener);
		}
		return pDialog;
	}

	public static byte[] getAssetsFileData(Context context, String fileName)
			throws Exception {
		byte[] ret = null;
		try {
			InputStream is = context.getAssets().open(fileName);
			byte[] data = new byte[2048];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int read;
			while ((read = is.read(data)) != -1) {
				baos.write(data, 0, read);
			}
			is.close();
			ret = baos.toByteArray();
			baos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static final ProgressDialog getProgressDialog(Context context) {
		ProgressDialog progressDialog = new ProgressDialog(context);
		// progressDialog.setTitle("Loading");
		// progressDialog.setMessage( "Please wait...");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setCancelable(true);
		progressDialog.setIndeterminate(true);
		return progressDialog;
	}

	public static boolean containsArabicLetters(String text) {
		for (int i = 0, len = text.length(); i < len; i++) {
			char character = text.charAt(i);
			if (character >= 0x600 && character <= 0x6ff) {
				return true;
			}
			if (character >= 0x750 && character <= 0x77f) {
				return true;
			}
			if (character >= 0xfb50 && character <= 0xfc3f) {
				return true;
			}
			if (character >= 0xfe70 && character <= 0xfefc) {
				return true;
			}
		}
		return false;
	}
	
	public static final String getException(Exception ex) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			PrintWriter pr = new PrintWriter(baos);
			ex.printStackTrace(pr);
			pr.close();
			byte[] ret = baos.toByteArray();
			baos.close();
			return new String(ret);
		} catch (Exception e) {
			return null;
		}
	}
}
